/****************************************************************************
*
*					Functions of class SimilarityFeature
*
****************************************************************************/

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <stdlib.h>

#include "DataSet.h"
#include "SimilarityFeature.h"
#include "ExtendedPGMStruct.h"


/****************************************************************************
*					SimilarityFeature::SimilarityFeature
*
*	Description:	Standard constructor with initialization of internal variables
*	Parameters:		-
*	Returns:		-
*
****************************************************************************/
SimilarityFeature::SimilarityFeature (void) {
	this->clear();
	this->id = 14;
	this->name.assign("Similarity");
}


/****************************************************************************
*					SimilarityFeature::~SimilarityFeature
*
*	Description:	Standard destructor with removing all dynamic internal variables
*	Parameters:		-
*	Returns:		-
*
****************************************************************************/
SimilarityFeature::~SimilarityFeature (void) {
	
}


/****************************************************************************
*					SimilarityFeature::calculate
*
*	Description:	Calculates feature value given variable domain and the data
*	Parameters:		varIDs - indexes of variables in 'vars' for which to 
*							 calculate the feature value
*					vars - all variables in the PGM
*	Returns:		0 - if features is not true (values of variables in PGM are
*						different from 'varValues')
*					1 - otherwise
*
****************************************************************************/
float SimilarityFeature::calculate (const std::vector<unsigned int>& varIDs, const std::vector <unsigned int>& vars) const {
	// DUMMY
	std::cout << "\nWarning in SimilarityFeature::calculate: dummy function is called.\n";
	return 0.0;
}


/****************************************************************************
*					SimilarityFeature::calculate
*
*	Description:	Calculates feature value given variable domain and the data
*	Parameters:		vars - variables for which to calculate feature value
*	Returns:		0 - if features is not true (values of variables in PGM are
*						different from 'varValues')
*					1 - otherwise
*
****************************************************************************/
float SimilarityFeature::calculate (const std::vector <unsigned int>& vars) const {
	// DUMMY
	std::cout << "\nWarning in SimilarityFeature::calculate: dummy function is called.\n";
	return 0.0;
}


/****************************************************************************
*					SimilarityFeature::calculate
*
*	Description:	Calculates feature value given PGM description
*	Parameters:		extPGMStruct - description of PGM to work on
*					varID - id of a variable in extPGMStruct for which (for which
*					nieghbors or local structure) to calculate the feature
*	Returns:		float - feature value in local network of variable varID
*						in extPGMStruct
*
****************************************************************************/
float SimilarityFeature::calculate (const ExtendedPGMStruct& extPGMStruct, const unsigned int& varID) const {
	bool isX;
	unsigned int i, j;
	extPGMStruct.getVarIndex(isX, i, j, varID);
	if ((!isX) || (extPGMStruct.variables[varID] != 2))
		return 0.0;
	//
	float toRet = 0.0;
	for (unsigned int k = 0; k < extPGMStruct.actors[i].attributes.size(); k++) {
		if (extPGMStruct.actors[i].attributes[k][j] == 1)
			toRet++;
	}

	return toRet / (float) extPGMStruct.actors[i].attributes.size();
}


/****************************************************************************
*					SimilarityFeature::setDesiredValues
*
*	Description:	Sets values for which a feature should return 1
*	Parameters:		desiredValues - ordered vector of desired values
*	Returns:		0
*
****************************************************************************/
int SimilarityFeature::setDesiredValues (const std::vector <unsigned int>& desiredValues) {
	// Not applicable to this class - not doing anything
	return 0;
}


/****************************************************************************
*					SimilarityFeature::getDesiredValues
*
*	Description:	Returns values for which a feature gives 1
*	Parameters:		desiredValues - a parameter that is set to the feature's
*						desired values of a domain of variables
*	Returns:		-1
*
****************************************************************************/
int SimilarityFeature::getDesiredValues (std::vector <unsigned int>& desiredValues) const {
	// Not applicable to this class - returning empty vector
	desiredValues.clear();
	return -1;
}


/****************************************************************************
*					SimilarityFeature::getObjectID
*
*	Description:	Returns ID of this type of features
*	Parameters:		-
*	Returns:		int - id of this class
*
****************************************************************************/
int SimilarityFeature::getObjectID (void) const {
	return this->id;
}


/****************************************************************************
*					SimilarityFeature::getObjectName
*
*	Description:	Returns string Name of this type of features
*	Parameters:		name - name of this class
*	Returns:		0
*
****************************************************************************/
int SimilarityFeature::getObjectName (std::string &nameStr) const {
	nameStr = this->name;
	return 0;
}


/****************************************************************************
*					SimilarityFeature::clear
*
*	Description:	Removes all variables in object internal variables
*	Parameters:		-
*	Returns:		-
*
****************************************************************************/
void SimilarityFeature::clear (void) {
	
}


/****************************************************************************
*					SimilarityFeature::operator==
*
*	Description:	Operator for checking whether two featres are the same
*	Parameters:		-
*	Returns:		-
*
****************************************************************************/
bool SimilarityFeature::operator==(Feature& second) const {
	if (this->id != second.getObjectID())
		return false;	
	return true;
}
